﻿// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

using Admin.NET.Application.Entity;
using Admin.NET.Core.Service;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;

/// <summary>
/// 优惠券服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class KerryplususercouponService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<Kerryplususercoupon> _kerryplususercouponRep;
    public KerryplususercouponService(SqlSugarRepository<Kerryplususercoupon> kerryplususercouponRep)
    {
        _kerryplususercouponRep = kerryplususercouponRep;
    }

    /// <summary>
    /// 分页查询优惠券
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    [DisplayName("分页查询优惠券")]
    public async Task<SqlSugarPagedList<KerryplususercouponOutput>> Page(PageKerryplususercouponInput input)
    {
		input.SearchKey = input.SearchKey?.Trim();
        var query = _kerryplususercouponRep.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(input.SearchKey), u =>
                u.UserId.Contains(input.SearchKey)
                || u.PhoneNumber.Contains(input.SearchKey)
                || u.CouponId.Contains(input.SearchKey)
                || u.CouponName.Contains(input.SearchKey)
                || u.CouponCode.Contains(input.SearchKey)
                || u.CouponState.Contains(input.SearchKey)
                || u.CouponType.Contains(input.SearchKey)
            )
            .WhereIF(!string.IsNullOrWhiteSpace(input.UserId), u => u.UserId.Contains(input.UserId.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.PhoneNumber), u => u.PhoneNumber.Contains(input.PhoneNumber.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.CouponId), u => u.CouponId.Contains(input.CouponId.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.CouponName), u => u.CouponName.Contains(input.CouponName.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.CouponCode), u => u.CouponCode.Contains(input.CouponCode.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.CouponState), u => u.CouponState.Contains(input.CouponState.Trim()))
            .WhereIF(input.ExchangeDateRange != null && input.ExchangeDateRange.Length == 2, u => u.ExchangeDate >= input.ExchangeDateRange[0] && u.ExchangeDate <= input.ExchangeDateRange[1])
            .WhereIF(input.Category>0, u => u.Category == input.Category)
            .WhereIF(!string.IsNullOrWhiteSpace(input.CouponType), u => u.CouponType.Contains(input.CouponType.Trim()))
            .WhereIF(input.CreateDateRange != null && input.CreateDateRange.Length == 2, u => u.CreateDate >= input.CreateDateRange[0] && u.CreateDate <= input.CreateDateRange[1])
            .WhereIF(input.ExchangeStartDateRange != null && input.ExchangeStartDateRange.Length == 2, u => u.ExchangeStartDate >= input.ExchangeStartDateRange[0] && u.ExchangeStartDate <= input.ExchangeStartDateRange[1])
            .WhereIF(input.ExchangeEndDateRange != null && input.ExchangeEndDateRange.Length == 2, u => u.ExchangeEndDate >= input.ExchangeEndDateRange[0] && u.ExchangeEndDate <= input.ExchangeEndDateRange[1])
            .Select<KerryplususercouponOutput>();
		return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加优惠券
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    [DisplayName("增加优惠券")]
    public async Task<long> Add(AddKerryplususercouponInput input)
    {
        var entity = input.Adapt<Kerryplususercoupon>();
        await _kerryplususercouponRep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除优惠券
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    [DisplayName("删除优惠券")]
    public async Task Delete(DeleteKerryplususercouponInput input)
    {
        var entity = await _kerryplususercouponRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _kerryplususercouponRep.FakeDeleteAsync(entity);   //假删除
        //await _kerryplususercouponRep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新优惠券
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    [DisplayName("更新优惠券")]
    public async Task Update(UpdateKerryplususercouponInput input)
    {
        var entity = input.Adapt<Kerryplususercoupon>();
        await _kerryplususercouponRep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取优惠券
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    [DisplayName("获取优惠券")]
    public async Task<Kerryplususercoupon> Detail([FromQuery] QueryByIdKerryplususercouponInput input)
    {
        return await _kerryplususercouponRep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取优惠券列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    [DisplayName("获取优惠券列表")]
    public async Task<List<KerryplususercouponOutput>> List([FromQuery] PageKerryplususercouponInput input)
    {
        return await _kerryplususercouponRep.AsQueryable().Select<KerryplususercouponOutput>().ToListAsync();
    }





}
